package net.sparrow.gui.drawing;

import java.util.ArrayList;
import java.util.HashMap;

import net.sparrow.data.UtilFile;
import net.sparrow.util.SpAnimation;
import net.sparrow.util.Util;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.lwjgl.opengl.GL11;



public class SpColor {

	private static HashMap<String, SpColor> colorList = new HashMap<String, SpColor>();


	public static void loadColors() {
		try {
			JSONObject obj = UtilFile.loadJSONFile("SparrowColors.json");
			for(Object k : obj.keySet()) {
				String key = (String)k;
				JSONArray data = (JSONArray)obj.get(key);
				SpColor color = 
						new SpColor(Float.parseFloat(data.get(0).toString()),Float.parseFloat(data.get(1).toString())
								,Float.parseFloat(data.get(2).toString()),Float.parseFloat(data.get(3).toString()));
				colorList.put(key,color);
			}
		} catch (Exception e) {
			//load defaults
			loadDefaultColors();
			e.printStackTrace();
			return;
		}
	}
	
	private static void loadDefaultColors() {
		
	}
	
	public static SpColor getColor(String id) {
		SpColor template = colorList.get(id);
		if(template != null) {
			return template.clone();
		}
		return null;
	}


	public float r;
	public float g;
	public float b;
	public SpAnimation anim = new SpAnimation(300d,100d);

	public SpColor(float r1, float g1, float b1) {
		r = r1;
		g = g1;
		b = b1;
		anim = new SpAnimation(1d,100d); //basically not animated
	}
	
	public SpColor(float r1, float g1, float b1, double dur) {
		this(r1,g1,b1);
		anim = new SpAnimation(dur,100d);
	}

	private float getAlpha() {
		return (float) (anim.getValue() * .01);
	}
	
	public void resetOpacityAnim() {
		anim.resetAnimation();
	}

	/**
	 * Get the color (accounting for the opening menu animation).
	 * @return
	 */
	public void setOGLColor() {
		GL11.glColor4f(r, g, b, getAlpha());
	}

	public int getColor() {
		float ab = (int)(getAlpha() * 255f) * 0x1000000;
		float rb =  (int)(r * 255f) * 0x10000;
		float gb = (int)(g * 255f) * 0x100;
		float bb = b * 255f;
		return (int) (ab + rb + gb + bb);
	}
	
	public SpColor clone() {
		return new SpColor(r,g,b,getAlpha());
	}


}
